package com.tywj.panda.driver.performance

import androidx.collection.ArrayMap
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.tywj.panda.customer.common.http.PDObjectCallback
import com.tywj.panda.customer.common.tool.MoneyTool
import com.tywj.panda.customer.common.view.paging.BasePagingActivitySearch
import com.tywj.panda.driver.R
import com.tywj.panda.driver.api.DriverApi
import com.tywj.panda.driver.entity.DriverPerformanceEntity
import com.tywj.panda.driver.entity.DriverTotalEntity
import com.tywj.panda.driver.route.DriverRoute
import com.tywj.lib.net.retrofit.request.BaseRequest
import com.tywj.panda.customer.common.tool.LogoutTool
import kotlinx.android.synthetic.main.driver_activity_performance.*

/**
 * 查看绩效
 */
@Route(path = DriverRoute.DRIVER_PERFORM)
class DriverPerformanceActivity :
    BasePagingActivitySearch<DriverPerformanceEntity, DriverPerformanceEntity.DataBean>(DriverPerformanceEntity.DataBean::class.java) {

    override fun getRecyclerViewAdapter(): BaseQuickAdapter<DriverPerformanceEntity.DataBean, *> {
        return DriverWaterAdapter()
    }

    override fun getRequest(): BaseRequest<*> = DriverApi.getBalanceWater()

    /**
     * 下拉刷新
     */
    override fun getRequestParams(): ArrayMap<String, String> = DriverApi.listWaterParams()

    /**
     * 加载更多
     */
    override fun getRequestParamsLoadMore(): ArrayMap<String, String> {
        return DriverApi.listWaterParams(mAdapter.data.last().createTime)
    }

    override fun getLayoutID(): Int = R.layout.driver_activity_performance

    override fun loadDataOnCreate() {
        super.loadDataOnCreate()
        loadDataTotal()
    }


    private fun loadDataTotal() {

        DriverApi.getBalance().attachToLifecycle(this)
            .execute(object : PDObjectCallback<DriverTotalEntity>(DriverTotalEntity::class.java) {
                override fun success(data: DriverTotalEntity) {
                    tvTotal.text = MoneyTool.formatFen(data.amout)
                }

                override fun error(code: Int, message: String) {
                    super.error(code, message)

                    //特殊处理，兼容服务器
                    when (code) {
                        -4, 401 -> {
                            LogoutTool.logout(R.string.common_account_login_token_is_expired)
                        }
                    }
                }
            })
    }
}